
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.io.qchem.utils &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.qchem.utils</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.io.qchem.utils</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Utilities for Qchem io.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>


<div class="viewcode-block" id="read_pattern"><a class="viewcode-back" href="../../../../pymatgen.io.qchem.utils.html#pymatgen.io.qchem.utils.read_pattern">[docs]</a><span class="k">def</span> <span class="nf">read_pattern</span><span class="p">(</span><span class="n">text_str</span><span class="p">,</span> <span class="n">patterns</span><span class="p">,</span> <span class="n">terminate_on_match</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">postprocess</span><span class="o">=</span><span class="nb">str</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    General pattern reading on an input string</span>

<span class="sd">    Args:</span>
<span class="sd">        text_str (str): the input string to search for patterns</span>
<span class="sd">        patterns (dict): A dict of patterns, e.g.,</span>
<span class="sd">            {&quot;energy&quot;: r&quot;energy\\(sigma-&gt;0\\)\\s+=\\s+([\\d\\-.]+)&quot;}.</span>
<span class="sd">        terminate_on_match (bool): Whether to terminate when there is at</span>
<span class="sd">            least one match in each key in pattern.</span>
<span class="sd">        postprocess (callable): A post processing function to convert all</span>
<span class="sd">            matches. Defaults to str, i.e., no change.</span>

<span class="sd">    Renders accessible:</span>
<span class="sd">        Any attribute in patterns. For example,</span>
<span class="sd">        {&quot;energy&quot;: r&quot;energy\\(sigma-&gt;0\\)\\s+=\\s+([\\d\\-.]+)&quot;} will set the</span>
<span class="sd">        value of matches[&quot;energy&quot;] = [[-1234], [-3453], ...], to the</span>
<span class="sd">        results from regex and postprocess. Note that the returned values</span>
<span class="sd">        are lists of lists, because you can grep multiple items on one line.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">compiled</span> <span class="o">=</span> <span class="p">{</span>
        <span class="n">key</span><span class="p">:</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="n">patterns</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
    <span class="p">}</span>
    <span class="n">matches</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="n">compiled</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">for</span> <span class="n">match</span> <span class="ow">in</span> <span class="n">pattern</span><span class="o">.</span><span class="n">finditer</span><span class="p">(</span><span class="n">text_str</span><span class="p">):</span>
            <span class="n">matches</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">postprocess</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">match</span><span class="o">.</span><span class="n">groups</span><span class="p">()])</span>
            <span class="k">if</span> <span class="n">terminate_on_match</span><span class="p">:</span>
                <span class="k">break</span>
    <span class="k">return</span> <span class="n">matches</span></div>


<div class="viewcode-block" id="read_table_pattern"><a class="viewcode-back" href="../../../../pymatgen.io.qchem.utils.html#pymatgen.io.qchem.utils.read_table_pattern">[docs]</a><span class="k">def</span> <span class="nf">read_table_pattern</span><span class="p">(</span><span class="n">text_str</span><span class="p">,</span>
                       <span class="n">header_pattern</span><span class="p">,</span>
                       <span class="n">row_pattern</span><span class="p">,</span>
                       <span class="n">footer_pattern</span><span class="p">,</span>
                       <span class="n">postprocess</span><span class="o">=</span><span class="nb">str</span><span class="p">,</span>
                       <span class="n">attribute_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                       <span class="n">last_one_only</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parse table-like data. A table composes of three parts: header,</span>
<span class="sd">    main body, footer. All the data matches &quot;row pattern&quot; in the main body</span>
<span class="sd">    will be returned.</span>

<span class="sd">    Args:</span>
<span class="sd">        text_str (str): the input string to search for patterns</span>
<span class="sd">        header_pattern (str): The regular expression pattern matches the</span>
<span class="sd">            table header. This pattern should match all the text</span>
<span class="sd">            immediately before the main body of the table. For multiple</span>
<span class="sd">            sections table match the text until the section of</span>
<span class="sd">            interest. MULTILINE and DOTALL options are enforced, as a</span>
<span class="sd">            result, the &quot;.&quot; meta-character will also match &quot;\n&quot; in this</span>
<span class="sd">            section.</span>
<span class="sd">        row_pattern (str): The regular expression matches a single line in</span>
<span class="sd">            the table. Capture interested field using regular expression</span>
<span class="sd">            groups.</span>
<span class="sd">        footer_pattern (str): The regular expression matches the end of the</span>
<span class="sd">            table. E.g. a long dash line.</span>
<span class="sd">        postprocess (callable): A post processing function to convert all</span>
<span class="sd">            matches. Defaults to str, i.e., no change.</span>
<span class="sd">        attribute_name (str): Name of this table. If present the parsed data</span>
<span class="sd">            will be attached to &quot;data. e.g. self.data[&quot;efg&quot;] = [...]</span>
<span class="sd">        last_one_only (bool): All the tables will be parsed, if this option</span>
<span class="sd">            is set to True, only the last table will be returned. The</span>
<span class="sd">            enclosing list will be removed. i.e. Only a single table will</span>
<span class="sd">            be returned. Default to be True.</span>

<span class="sd">    Returns:</span>
<span class="sd">        List of tables. 1) A table is a list of rows. 2) A row if either a list of</span>
<span class="sd">        attribute values in case the the capturing group is defined without name in</span>
<span class="sd">        row_pattern, or a dict in case that named capturing groups are defined by</span>
<span class="sd">        row_pattern.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">table_pattern_text</span> <span class="o">=</span> <span class="n">header_pattern</span> <span class="o">+</span> \
        <span class="sa">r</span><span class="s2">&quot;\s*(?P&lt;table_body&gt;(?:&quot;</span> <span class="o">+</span> <span class="n">row_pattern</span> <span class="o">+</span> <span class="sa">r</span><span class="s2">&quot;)+)\s*&quot;</span> <span class="o">+</span> <span class="n">footer_pattern</span>
    <span class="n">table_pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">table_pattern_text</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span> <span class="o">|</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
    <span class="n">rp</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">row_pattern</span><span class="p">)</span>
    <span class="n">data</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">tables</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">mt</span> <span class="ow">in</span> <span class="n">table_pattern</span><span class="o">.</span><span class="n">finditer</span><span class="p">(</span><span class="n">text_str</span><span class="p">):</span>
        <span class="n">table_body_text</span> <span class="o">=</span> <span class="n">mt</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s2">&quot;table_body&quot;</span><span class="p">)</span>
        <span class="n">table_contents</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">ml</span> <span class="ow">in</span> <span class="n">rp</span><span class="o">.</span><span class="n">finditer</span><span class="p">(</span><span class="n">table_body_text</span><span class="p">):</span>
            <span class="n">d</span> <span class="o">=</span> <span class="n">ml</span><span class="o">.</span><span class="n">groupdict</span><span class="p">()</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">processed_line</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">postprocess</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">processed_line</span> <span class="o">=</span> <span class="p">[</span><span class="n">postprocess</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">ml</span><span class="o">.</span><span class="n">groups</span><span class="p">()]</span>
            <span class="n">table_contents</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">processed_line</span><span class="p">)</span>
        <span class="n">tables</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">table_contents</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">last_one_only</span><span class="p">:</span>
        <span class="n">retained_data</span> <span class="o">=</span> <span class="n">tables</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">retained_data</span> <span class="o">=</span> <span class="n">tables</span>
    <span class="k">if</span> <span class="n">attribute_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">data</span><span class="p">[</span><span class="n">attribute_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">retained_data</span>
        <span class="k">return</span> <span class="n">data</span>
    <span class="k">return</span> <span class="n">retained_data</span></div>


<div class="viewcode-block" id="lower_and_check_unique"><a class="viewcode-back" href="../../../../pymatgen.io.qchem.utils.html#pymatgen.io.qchem.utils.lower_and_check_unique">[docs]</a><span class="k">def</span> <span class="nf">lower_and_check_unique</span><span class="p">(</span><span class="n">dict_to_check</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Takes a dictionary and makes all the keys lower case. Also replaces</span>
<span class="sd">    &quot;jobtype&quot; with &quot;job_type&quot; just so that key specifically can be called</span>
<span class="sd">    elsewhere without ambiguity. Finally, ensures that multiple identical</span>
<span class="sd">    keys, that differed only due to different capitalizations, are not</span>
<span class="sd">    present. If there are multiple equivalent keys, an Exception is raised.</span>

<span class="sd">    Args:</span>
<span class="sd">        dict_to_check (dict): The dictionary to check and standardize</span>

<span class="sd">    Returns:</span>
<span class="sd">        to_return (dict): An identical dictionary but with all keys made</span>
<span class="sd">            lower case and no identical keys present.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">dict_to_check</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">to_return</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">dict_to_check</span><span class="p">:</span>
            <span class="n">new_key</span> <span class="o">=</span> <span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">new_key</span> <span class="o">==</span> <span class="s2">&quot;jobtype&quot;</span><span class="p">:</span>
                <span class="n">new_key</span> <span class="o">=</span> <span class="s2">&quot;job_type&quot;</span>
            <span class="k">if</span> <span class="n">new_key</span> <span class="ow">in</span> <span class="n">to_return</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">to_return</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">!=</span> <span class="n">to_return</span><span class="p">[</span><span class="n">new_key</span><span class="p">]:</span>
                    <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span>
                        <span class="s2">&quot;Multiple instances of key &quot;</span> <span class="o">+</span> <span class="n">new_key</span> <span class="o">+</span> <span class="s2">&quot; found with different values! Exiting...&quot;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">to_return</span><span class="p">[</span><span class="n">new_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">dict_to_check</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
                <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                    <span class="n">to_return</span><span class="p">[</span><span class="n">new_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">dict_to_check</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">to_return</span></div>


<div class="viewcode-block" id="process_parsed_coords"><a class="viewcode-back" href="../../../../pymatgen.io.qchem.utils.html#pymatgen.io.qchem.utils.process_parsed_coords">[docs]</a><span class="k">def</span> <span class="nf">process_parsed_coords</span><span class="p">(</span><span class="n">coords</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Takes a set of parsed coordinates, which come as an array of strings,</span>
<span class="sd">    and returns a numpy array of floats.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">geometry</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">coords</span><span class="p">),</span> <span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">ii</span><span class="p">,</span> <span class="n">entry</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">coords</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">jj</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
            <span class="n">geometry</span><span class="p">[</span><span class="n">ii</span><span class="p">,</span> <span class="n">jj</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="n">jj</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">geometry</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.qchem.utils</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>